package com.example.zk2

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.zk2.databinding.ActivityMainBinding
import com.example.zk2.db.AppDbUtils
import com.example.zk2.mvvm.BaseActivity

class MainActivity : BaseActivity<ActivityMainBinding,MainViewModel>() {
    override fun getLayout(): Int  = R.layout.activity_main

    override fun getViewModel(): Class<MainViewModel>  = MainViewModel::class.java

    override fun initView() {
        binding.login.setOnClickListener {
            viewModel.login(binding.username.text.toString(),binding.password.text.toString())
        }
    }

    override fun initData() {
        viewModel.success.observe(this){
            ToastUtils.showLong(it.message)
            if(it.code==200){
                SPUtils.getInstance().put("token",it.data.token)
                //保存数据库
                AppDbUtils.getAppDb().getUserDao().insertUser(it.data)
                ARouter.getInstance().build("/app/ShopActivity").navigation()
            }
        }
    }

}